<template>
  <MainSlot>
    <template v-slot:searchForm>
      <SearchForm v-if="formItemList.length" :formItemList="formItemList" :unfoldFlag="unfoldFlag" @search="search" @isUnfold="
          (v) => {
            unfoldFlag = v;
          }
        ">
      </SearchForm>
    </template>
    <template v-slot:table>
      <TableData :tableThead="tableThead" :dataList="dataList" :loading="loading">
        <template v-slot:fbillflag="{ row }">
          <span>{{ queryfbillflagList(row.fbillflag) }}</span>
        </template>
        <template v-slot:seeDetail="{ row }">
          <span class="successText" @click="lookView(row)">查看详情</span>
        </template>
      </TableData>
    </template>
    <template v-slot:pagination>
      <Pagination @current-change="handleCurrentChange" :size="size" :page="page" :total="total">
      </Pagination>
    </template>
    <template v-slot:dialogModule>
      <el-dialog title="转塘详情" :visible.sync="dialogVisibleLookView" width="70%" :before-close="
          () => {
            dialogVisibleLookView = false;
          }
        ">
        <el-row :gutter="15">
          <el-col v-show="!item.slotName" v-for="(item, index) in tableThead" :key="index" :span="6" style="margin: 7px 0px">
            <span>{{ item.label }}：</span>
            <span>{{ lookViewObj[item.prop] }}</span>
          </el-col>
        </el-row>
        <el-table :data="tableData" border style="width: 100%" height="400">
          <el-table-column v-for="(item, index) in dialogtableThead" :key="index" :prop="item.prop" :label="item.label" min-width="100" align="center">
          </el-table-column>
        </el-table>
        <span slot="footer" class="dialog-footer">
          <el-button @click="(dialogVisibleLookView = false), (lookViewObj = {})">关 闭</el-button>
        </span>
      </el-dialog>
    </template>
  </MainSlot>
</template>
<script>
import MainSlot from "@/components-production-management/MainSlot";
import Pagination from "@/components-production-management/Pagination";
import SearchForm from "@/components-production-management/SearchForm";
import TableData from "@/components-production-management/TableData";
//NC批次
import { fbillflagList } from "@/utils/NcQueryData";
import ncproductionApi from "@/api/production-management-nc/index";
import { getOneYearAgo, getCurrentDate } from "@/utils/time";
import ncBasicInfoApi from "@/api/nc-basic-info/index";

export default {
  components: { MainSlot, Pagination, SearchForm, TableData },
  data() {
    return {
      unfoldFlag: false,
      loading: false,
      page: 1,
      size:30,
      total: 10,
      searchFormData: {
        startDate: getOneYearAgo(),
        endDate: getCurrentDate(),
      },
      dataList: [],
      dialogVisibleLookView: false,
      tableData: [],
      lookViewObj: {},
      dialogtableThead: [
        {
          label: "物料编码",
          prop: "cmaterialvcode",
        },
        {
          label: "物料编码名称",
          prop: "cmaterialvname",
        },
        {
          label: "单位",
          prop: "castunitname",
        },
        {
          label: "主单位",
          prop: "cunitname",
        },
        {
          label: "换算率",
          prop: "vchangerate",
        },
        {
          label: "批次号",
          prop: "vbatchcode",
        },
        {
          label: "应转数量",
          prop: "nassistnum",
        },
        {
          label: "应转主数量",
          prop: "nnum",
        },
        {
          label: "活鱼品规",
          prop: "vfree1name",
        },
      ],
      tableThead: [
        // { isShow: true, label: '批次编号', prop: 'batchName' },

        {
          isShow: true,
          label: "单据号",
          prop: "vbillcode",
          minWidth: "120px",
        },
        {
          isShow: true,
          label: "单据日期",
          prop: "dbilldate",
          minWidth: "120px",
        },
        {
          isShow: true,
          label: "出库仓库",
          prop: "cwarehousename",
          minWidth: "120px",
        },
        {
          isShow: true,
          label: "入库仓库",
          prop: "cotherwhname",
          minWidth: "120px",
        },
        {
          isShow: true,
          label: "出库部门",
          prop: "cdptname",
          minWidth: "120px",
        },
        {
          isShow: true,
          label: "入库部门",
          prop: "cotherdptname",
          minWidth: "120px",
        },
        {
          isShow: true,
          label: "总数量(条)",
          prop: "ntotalnum",
          minWidth: "120px",
        },
        {
          isShow: true,
          label: "查看详情",
          slotName: "seeDetail",
          minWidth: "120px",
        },
      ],

      formItemList: [
        {
          label: "单据号",
          type: "input",
          param: "vbillcode",
          multiple: false,
          isShow: true,
        },
        {
          label: "批次号",
          type: "input",
          param: "vbatchcode",
          multiple: false,
          isShow: true,
        },
        {
          label: "出库仓库",
          type: "select",
          selectOptions: [],
          param: "cwarehouseid",
          defaultSelect: "", // 下拉框默认选中项
          multiple: false,
          isShow: true,
        },
        {
          label: "入库仓库",
          type: "select",
          selectOptions: [],
          param: "cotherwhid",
          defaultSelect: "", // 下拉框默认选中项
          multiple: false,
          isShow: true,
        },
        {
          label: "出库部门名称",
          type: "select",
          selectOptions: [],
          param: "cdptid",
          defaultSelect: "", // 下拉框默认选中项
          multiple: false,
          isShow: true,
        },
        {
          label: "入库部门名称",
          type: "select",
          selectOptions: [],
          param: "cotherdptid",
          defaultSelect: "", // 下拉框默认选中项
          multiple: false,
          isShow: true,
        },

        {
          label: "日期查询",
          type: "daterange",
          param: "dateTime",
          defaultSelect: [getOneYearAgo(), getCurrentDate()],
          multiple: false,
          isShow: true,
        },
      ],
      StordocList: [],
      OrgDeptList: [],
    };
  },
  mounted() {
    this.queryPageByCondition();
    this.queryStordoc();
    this.queryOrgDept();
  },
  methods: {
    queryStordoc() {
      ncBasicInfoApi.queryStordoc({}).then((res) => {
        this.StordocList = res.data;
        this.setFormOptions();
      });
    },
    queryOrgDept() {
      ncBasicInfoApi.queryOrgDept({}).then((res) => {
        this.OrgDeptList = res.data;
        this.setFormOptions();
      });
    },
    setFormOptions() {
      let list = JSON.parse(JSON.stringify(this.formItemList));
      this.formItemList = [];
      list.forEach((item, index) => {
        if (
          item.param.indexOf("cotherwhid") !== -1 ||
          item.param.indexOf("cwarehouseid") !== -1
        ) {
          let selectOptions = this.StordocList.map((data) => {
            return {
              text: data.name,
              value: data.pk_stordoc,
            };
          });
          item.selectOptions = selectOptions;
        }
        if (
          item.param.indexOf("cotherdptid") !== -1 ||
          item.param.indexOf("cdptid") !== -1
        ) {
          let selectOptions = this.OrgDeptList.map((data) => {
            return {
              text: data.name,
              value: data.pk_dept,
            };
          });
          item.selectOptions = selectOptions;
        }
      });
      this.$nextTick(() => {
        this.$set(this, "formItemList", list);
      });
    },

    queryPageByCondition() {
      let params = {
        page: this.page,
        size: this.size,
        entity: {
          ...this.searchFormData,
        },
      };
      this.loading = true;
      ncproductionApi
        .queryWhstrans(params)
        .then((res) => {
          this.loading = false;
          if (res.success == 1) {
            this.dataList = res.data.content || [];
            this.total = res.data.totalElements;
            if (res.data.totalPages && this.page > res.data.totalPages) {
              this.page = res.data.totalPages;
              this.queryPageByCondition();
            }
          } else {
            this.$message.warning(res.message || "分页查询失败");
          }
        })
        .catch((err) => {
          this.loading = false;
          console.log(err);
        });
    },
    search(data) {
      this.page = 1;
      if (data.dateTime && data.dateTime.length) {
        data.startDate = data.dateTime[0];
        data.endDate = data.dateTime[1];
      }
      delete data.dateTime;
      this.searchFormData = { ...data };

      for (let key in this.searchFormData) {
        if (!this.searchFormData[key]) {
          this.searchFormData[key] = null;
        }
      }
      console.log(this.searchFormData, data, "this.searchFormData");
      this.queryPageByCondition();
    },
    handleCurrentChange(page) {
      this.page = page;
      this.queryPageByCondition();
    },

    //查看详情
    async lookView(row) {
      let obj = JSON.parse(JSON.stringify(row));
      let res = await ncproductionApi.queryWhstransDtl({
        orderId: obj.cspecialhid,
      });
      this.lookViewObj = obj;
      if (res.success) {
        this.tableData = res.data || [];
        this.dialogVisibleLookView = true;
      }
    },
    queryfbillflagList(e) {
      return fbillflagList(e);
    },
  },
};
</script>
<style lang="scss" scoped></style>